# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: Exp $

WANT_AUTOCONF="2.1"
inherit flag-o-matic toolchain-funcs eutils mozconfig-minefield makeedit multilib mercurial cvs fdo-mime autotools


DESCRIPTION="Firefox Web Browser"
HOMEPAGE="http://www.mozilla.org/projects/firefox/"

KEYWORDS="~amd64 ~x86"
SLOT="0"
LICENSE="MPL-1.1 GPL-2 LGPL-2.1"
IUSE="mozdevelop"

RDEPEND=" >=sys-devel/binutils-2.16.1
	>=dev-libs/nss-3.12.0_rc7
	>=dev-libs/nspr-4.7.0_rc4
	>=app-text/hunspell-1.1.9
	>=media-libs/lcms-1.17
	>=net-libs/xulrunner-9999"
#	>=dev-db/sqlite-3.3.17"

DEPEND="${RDEPEND}
	dev-util/pkgconfig"

S=${WORKDIR}/cvs-trunk-mirror

# Needed by src_compile() and src_install().
# Would do in pkg_setup but that loses the export attribute, they
# become pure shell variables.
export MOZ_CO_PROJECT=browser
export BUILD_OFFICIAL=1
export MOZILLA_OFFICIAL=1

src_unpack() {

	EHG_REPO_URI="http://hg.mozilla.org/cvs-trunk-mirror/"
	EHG_PROJECT="mozilla"
	mercurial_src_unpack
	
	#basis settings
	ECVS_SERVER="cvs-mirror.mozilla.org:/cvsroot"
	ECVS_PASS="anonymous"
	ECVS_MODULE="mozilla"
	ECVS_UP_OPT="-dPAC"
	ECVS_CO_OPT="-PA"
	ECVS_CVS_COMMAND="cvs -f -z3"
	ECVS_BRANCH="HEAD"
	MMODULES="/extensions/typeaheadfind \
			/extensions/venkman \
			/nsprpub/config"

	for RMM in ${MMODULES}; do
		ECVS_MODULE="mozilla${RMM}"
		cvs_src_unpack || die "cvs failed"
	done
	cp -Rf "${WORKDIR}"/mozilla/* "${WORKDIR}"/cvs-trunk-mirror/ 
	cd ${S} || die "cd failed"

	####################################
	#
	# Branding
	#
	####################################

	sed -i -e 's|MOZ_APP_NAME=firefox|MOZ_APP_NAME=minefield|' "${S}"/browser/confvars.sh
	sed -i -e 's|Name=Firefox|Name=Minefield|' "${S}"/browser/app/application.ini
	sed -i -e "s|Firefox|Minefield|" "${S}"/other-licenses/branding/firefox/locales/en-US/brand.*

	####################################
	#
	# architecture patches
	#
	####################################


	####################################
	#
	# general compilation and run-time fixes
	#
	####################################

	#correct the cairo/glitz mess, if using system libs
	epatch "${FILESDIR}"/666_mozilla-glitz-cairo-v2.patch
	#add the standard gentoo plugins dir
	epatch "${FILESDIR}"/064_firefox-nsplugins-v3.patch
	#make it use the system iconv
	epatch "${FILESDIR}"/165_native_uconv.patch
	#make it use system hunspell and correct the loading of dicts
	#thanks armin76 for the updated patch
	#thanks go to DoDo and Walter for the updated patch
	epatch "${FILESDIR}"/100-system-hunspell-corrections.patch
	#make loading certs behave with system nss
	epatch "${FILESDIR}"/068_firefox-nss-gentoo-fix.patch
	#
	epatch "${FILESDIR}"/667_typeahead-broken-v2.patch
	#make minefield build against xulrunner
	epatch "${FILESDIR}"/999_minefield_against_xulrunner-v2.patch


	####################################
	#
	# behavioral fixes
	#
	####################################

	#rpath patch
	epatch "${FILESDIR}"/063_firefox-rpath-3.patch
	#make minefield think it is firefox 
	epatch "${FILESDIR}"/777-teach-it-to-be-firefox.patch
	eautoreconf || die "failed  running eautoreconf"
}

src_compile() {
	declare MOZILLA_FIVE_HOME="/usr/$(get_libdir)/${PN}"
	MEXTENSIONS="default,typeaheadfind"

	#if use xforms; then
	#	MEXTENSIONS="${MEXTENSIONS},xforms"
	#fi
	####################################
	#
	# mozconfig, CFLAGS and CXXFLAGS setup
	#
	####################################

	mozconfig_init
	mozconfig_config

	mozconfig_annotate '' --enable-extensions="${MEXTENSIONS}"
	mozconfig_annotate '' --disable-mailnews
	mozconfig_annotate 'broken' --disable-mochitest
	mozconfig_annotate 'broken' --disable-crashreporter
	mozconfig_annotate '' --enable-native-uconv
	mozconfig_annotate '' --enable-system-hunspell
	#mozconfig_annotate '' --enable-system-sqlite3
	mozconfig_annotate '' --enable-image-encoder=all
	mozconfig_annotate '' --enable-canvas
	mozconfig_annotate '' --with-system-nspr
	mozconfig_annotate '' --with-system-nss
	mozconfig_annotate '' --enable-system-lcms
	mozconfig_annotate '' --enable-oji --enable-mathml
	mozconfig_annotate 'places' --enable-storage --enable-places --enable-places_bookmarks

	# Other ff-specific settings
	#mozconfig_use_enable mozdevelop jsd
	#mozconfig_use_enable mozdevelop xpctools
	mozconfig_use_extension mozdevelop venkman
	mozconfig_annotate '' --with-default-mozilla-five-home=${MOZILLA_FIVE_HOME}
	# Add xulrunner variable
	mozconfig_annotate '' --with-libxul-sdk

	# Finalize and report settings
	mozconfig_final

	# -fstack-protector breaks us
	if gcc-version ge 4 1; then
		gcc-specs-ssp && append-flags -fno-stack-protector
	else
		gcc-specs-ssp && append-flags -fno-stack-protector-all
	fi
	filter-flags -fstack-protector -fstack-protector-all

	####################################
	#
	#  Configure and build
	#
	####################################

	CPPFLAGS="${CPPFLAGS} -DARON_WAS_HERE" \
	CC="$(tc-getCC)" CXX="$(tc-getCXX)" LD="$(tc-getLD)" \
	econf || die

	# It would be great if we could pass these in via CPPFLAGS or CFLAGS prior
	# to econf, but the quotes cause configure to fail.
	sed -i -e \
		's|-DARON_WAS_HERE|-DGENTOO_NSPLUGINS_DIR=\\\"/usr/'"$(get_libdir)"'/nsplugins\\\" -DGENTOO_NSBROWSER_PLUGINS_DIR=\\\"/usr/'"$(get_libdir)"'/nsbrowser/plugins\\\"|' \
		"${S}"/config/autoconf.mk \
		"${S}"/toolkit/content/buildconfig.html

	# This removes extraneous CFLAGS from the Makefiles to reduce RAM
	# requirements while compiling
	edit_makefiles

	emake -j1 || die
}

pkg_preinst() {
	declare MOZILLA_FIVE_HOME="/usr/$(get_libdir)/${PN}"

	einfo "Removing old installs with some really ugly code.  It potentially"
	einfo "eliminates any problems during the install, however suggestions to"
	einfo "replace this are highly welcome.  Send comments and suggestions to"
	einfo "mozilla@gentoo.org."
	rm -rf "${ROOT}"${MOZILLA_FIVE_HOME}
}

src_install() {
	declare MOZILLA_FIVE_HOME="/usr/$(get_libdir)/${PN}"
	PKG_CONFIG=`which pkg-config`
	X_DATE=`date +%Y%m%d`
	XULRUNNER_VERSION=`${PKG_CONFIG} --modversion xulrunner-xpcom`
	XULRUNNER=`which xulrunner`

	# Most of the installation happens here
	dodir "${MOZILLA_FIVE_HOME}"
	cp -RL "${S}"/dist/bin/* "${D}"/"${MOZILLA_FIVE_HOME}"/ || die "cp failed"

	# Install icon and .desktop for menu entry
	insinto /usr/share/pixmaps
	doins ${FILESDIR}/icon/minefield.png

	# Fix bug 54179: Install .desktop file into /usr/share/applications
	# instead of /usr/share/gnome/apps/Internet (18 Jun 2004 agriffis)
	insinto /usr/share/applications
	doins ${FILESDIR}/icon/minefield.desktop

	####################################
	#
	# Some preferences, probably gentoo.org as start-page also
	#
	####################################

	dodir ${MOZILLA_FIVE_HOME}/greprefs
	cp ${FILESDIR}/gentoo-default-prefs.js ${D}${MOZILLA_FIVE_HOME}/greprefs/all-gentoo.js
	dodir ${MOZILLA_FIVE_HOME}/defaults/pref
	cp ${FILESDIR}/gentoo-default-prefs.js ${D}${MOZILLA_FIVE_HOME}/defaults/pref/all-gentoo.js
	#set the application.ini
	sed -i -e "s|BuildID=.*$|BuildID=${X_DATE}GentooMinefield|"	"${D}"/usr/$(get_libdir)/${PN}/application.ini
	sed -i -e "s|MinVersion=.*$|MinVersion=${XULRUNNER_VERSION}|" "${D}"/usr/$(get_libdir)/${PN}/application.ini
	sed -i -e "s|MaxVersion=.*$|MaxVersion=${XULRUNNER_VERSION}|" "${D}"/usr/$(get_libdir)/${PN}/application.ini
	echo "#!/bin/bash" > "${T}"/minefield
	echo "${XULRUNNER} ${MOZILLA_FIVE_HOME}/application.ini \"\$@\"" >> "${T}"/minefield

	into /usr
	dobin ${T}/minefield
	#setting the script
	#dodir /usr/bin
	#dosym ${D}${MOZILLA_FIVE_HOME}/minefield /usr/bin/minefield
}

pkg_postinst() {
	declare MOZILLA_FIVE_HOME="/usr/$(get_libdir)/${PN}"

	# Update mimedb for the new .desktop file
	fdo-mime_desktop_database_update
}
